package cn.wolfcode.trip.app.util;

import cn.wolfcode.trip.base.domain.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public abstract class UserContext {

    public static final String USER_IN_SESSION = "USER_IN_SESSION";

    //获取session对象
    public static HttpSession getSession(){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attributes.getRequest().getSession();
    }
    //将当前用户添加到session中
    public static void setCurrentUser(User currentUser){
        getSession().setAttribute(USER_IN_SESSION, currentUser);
    }
    //获取session中当前登录对用户
    public static User getCurrentUser(){
        return (User) getSession().getAttribute(USER_IN_SESSION);
    }

}
